/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pluginPanel;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

/**
 *
 * @author mazid
 */
public class MiniFrame extends JFrame implements ActionListener, ItemListener{
    private JPanel combo, text, bas;
    private JComboBox comboBox;
    private String[] items = {"Selectionner une fonction","Somme", "Moyenne", "Tri", "Moyenne ponderé", "Minimum","Maximum"};
    private JLabel label;
    private JButton valider,annuler;
    private static int[] cols;
    private static int[] rows;
    //private static Table table;
    public MiniFrame(){
        comboBox = new JComboBox(items);
        comboBox.addItemListener(this);
        label = new JLabel();
        text = new JPanel();
        combo = new JPanel();
        getContentPane().add( combo, "West");
        getContentPane().add(text);
        combo.add(comboBox);
        text.add(label);
        bas = new JPanel();
        valider = new JButton("Valider");
        valider.addActionListener(this);
        valider.setActionCommand("valider");
        annuler = new JButton("Annuler");
        annuler.addActionListener(this);
        annuler.setActionCommand("annuler");
        bas.add(valider);
        bas.add(annuler);
        getContentPane().add(bas, "South");
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setSize(600, 150);
        setLocationRelativeTo(null);
        setResizable(true);
        setVisible(true);
        
    }
    
    @Override
    public void itemStateChanged(ItemEvent e) {
        JComboBox comb = (JComboBox)e.getSource();
        String name = (String)comb.getSelectedItem();
        updateLabel(name);
        
    }
    
    // fonction qui mais a jour le texte du label 
    protected void updateLabel(String name){
        if(name.equalsIgnoreCase("somme")){
            label.setText("<html>Fonction "+name+" <br/>Elle a bésoin d'une plage de cellule</html>");
            
        }
        if(name.equalsIgnoreCase("moyenne")){
            label.setText("Fonction "+name);
        }
            
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        
        if(e.getActionCommand().equals("annuler")){
            
            this.dispose();
        }
        if(e.getActionCommand().equals("valider")){
            if(comboBox.getSelectedIndex()!=0){
                String name = (String)comboBox.getSelectedItem();
                
                PanelHaut.setNameFonction(name);
                this.dispose();
            }
            else{
                
                label.setText("Vous devez Selectionner une fonction !!!");
                
            }
            
            
        }
    }
    
    
}
